<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use Hash;
use Config;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class GoodsController extends Controller
{
    public function getIndex(Request $request)
   {
      /**用户添加页面
       * 
       */
      
      //解析模板
      $goods = DB::table('cates')
            ->leftJoin('goods', 'goods.cateid', '=','cates.id')
            
      // $goods = DB::select('select goods.id as gid,goods.*,cates.*,cates.id as tid from goods left join cates on goods.cateid=cates.id')
      
      ->where(function($query)use($request){$query->where('proname','like','%'.$request->input('keywords').'%');})

      ->paginate($request->input('num',10));

     //dd($goods);

      return view('goods.index',['goods'=>$goods,'request'=>$request->all()]);
        /// return view('goods.index');
   }
   


   //商品的修改
   

   public function getEdit($id)
   {
      //修改显示
      $res = DB::table('goods')->where('id',$id)->first();

       $cates = CateController::getCates();

      //显示模板 分配变量
      return view('goods.edit',['goodsinfo'=>$res,'cates'=>$cates]);
      
   }

   //数据的修改
   public function postUpdate(Request $request)
   {
      //接受数据
     $date = $request->except(['_token']);
     //图片上传
     if($request->hasFile('image')){
         //获取文件上传的后缀名
         $houzhuim = $request->file('image')->getClientOriginalExtension();
         //文件名
         $fileName = md5(time().rand(1,100)).'.'.$houzhuim;
         //获取上传图片路径
         $request->file('image')->move(config::get('app.upload_dir'),$fileName);

         $date['image'] = trim(config::get('app.upload_dir').$fileName,'.');

      }
      $id = $request->input('id');
      //判断商品详情

        if(!($request->des))
          {
              $des = DB::table('goods')->where('id',$id)->first();
             $date['des'] = $des->des;
          };
    //dd($request->des);
  // dd($date);
     
      //执行修改
      $res = DB::table('goods')->where('id',$id)->update($date);

      if($res){
         return redirect('/admin/goods/index')->with('success','数据修改成功');
       }else{
         //失败 闪存数据
         return back()->with('error','数据修改失败');
       }
   }

   //商品的删除操作

   public function getDelete($id)
   {
      //执行删除
      $res = DB::table('goods')->where('id',$id)->delete();
      if($res){
         return redirect('/admin/goods/index')->with('success','数据删除成功');
       }else{
         //失败 闪存数据
         return back()->with('error','数据删除失败');
       }

   }

   
   
   public function getAdd()
   {
   	/**用户添加页面
   	 * 
   	 */
   	//echo "51313";
   	//解析模板
       $cates = CateController::getCates();
       //dd($cates);
   	return view('goods.add',['cates'=>$cates]);
   }
   //商品图片上传



   
    public function postInsert(Request $request)
   {
      /**商品添加页面
       * 
       */
     //dd($request->all());
      //数据处理

      $this->validate($request,
     
        [

         'proname.required'=>'商品名不能为空',
         
         'image.required'=>'商品图片不能为空',
         'des.required'=>'商品描述不能为空',
        ]);

      $date = $request->except(['_token']);
       //dd($date);
      //商品图片上传
      if($request->hasFile('image')){
         //获取文件上传的后缀名
         $houzhuim = $request->file('image')->getClientOriginalExtension();
         //文件名
         $fileName = md5(time().rand(1,100)).'.'.$houzhuim;
         //获取上传图片路径
         $request->file('image')->move(config::get('app.upload_dir'),$fileName);

         $date['image'] = trim(config::get('app.upload_dir').$fileName,'.');

      }

     
     $date['addtime'] = date('Y-m-d H:i:s',time());
    //dd($date);
     $res = DB::table('goods')->insert($date);
      
     if($res){
         return redirect('admin/goods/index')->with('success','商品添加成功');
       }else{
         //失败 闪存数据
         return back()->with('error','商品添加插入失败');
       }

   }
   //商品详情展示


   public function getDes($id)
   {
       //解析模板
      $goods = DB::table('goods')->where('id',$id)->get();
        

       // dd($goods);
      return view('goods.des',['goods'=>$goods]);
      
   }


   //******************************************************************//
   /**
    * 商品套餐管理
    */
   //商品套餐添加
   public function getPackageadd(Request $request)
   {
    //echo "2222";
    $goods = DB::table('goods')->get();
    ///dd($goods);
    return view('goods.packageadd',['goods'=>$goods]);
   }


   //商品套餐数据插入
   public function postPackageinsert(Request $request)
   {
    //echo '214214';

    $this->validate($request,[
      'xprice'=>'required|regex:[\d+]',
      'yprice'=>'required|regex:[\d+]',
      'tname'=>'required'
         ],[
        
         'xprice.regex'=>'价格必须为数字',
         'yprice.regex'=>'价格必须为数字',
         'xprice.required'=>'价格不能为空',
         'yprice.required'=>'价格不能为空',
         'tname.required' =>'套餐名不能为空'
         ]);
      $date = $request->except(['_token']);
       //商品图片上传
      if($request->hasFile('packageimage')){
         //获取文件上传的后缀名
         $houzhuim = $request->file('packageimage')->getClientOriginalExtension();
         //文件名
         $fileName = md5(time().rand(1,100)).'.'.$houzhuim;
         //获取上传图片路径
         $request->file('packageimage')->move(config::get('app.upload_dir'),$fileName);

         $date['packageimage'] = trim(config::get('app.upload_dir').$fileName,'.');

      }
      $id =$date['sgsid'];
      $pro = DB::table('goods')->where('id',$id)->first();
      //dd($pro);
      $packagepro = $pro->proname;
      $date['packagepro'] = $packagepro;
      $date['addtime'] = date('Y-m-d H:i:s',time());


       //执行添加
      $res = DB::table('package')->insert($date);
      //dd($res);

     if($res){
         return redirect('admin/goods/packagelist')->with('success','商品添加成功');
       }else{
         //失败 闪存数据
         return back()->with('error','商品添加插入失败');
       }

     //dd($date);

   }



   //商品套餐列表
   public function getPackagelist(Request $request)
   {
    //查询套餐库
    $package = DB::table('package')
    ->where(function($query)use($request){$query->where('tname','like','%'.$request->input('keywords').'%');})

      ->paginate($request->input('num',10));

  //dd($package);

    return view('goods.packagelist',['package'=>$package,'request'=>$request->all()]);

   }
   //商品套餐删除

   public function getPackagedel($id)
   {
      //执行删除
      $res = DB::table('package')->where('id',$id)->delete();
      if($res){
         return redirect('/admin/goods/packagelist')->with('success','套餐删除成功');
       }else{
         //失败 闪存数据
         return back()->with('error','套餐删除失败');
       }

   }
//商品套餐修改packageedit
   public function getPackageedit($id)
   {
      //修改
      $res = DB::table('package')->where('id',$id)->first();
      $goods = DB::table('goods')->get();
      

      //显示模板 分配变量
        //dd($res);
      return view('goods.packageedit',['packageinfo'=>$res,'goods'=>$goods]);
   }

   //商品套餐的修改

      public function postPackageupdate(Request $request)
      {
        $date = $request->except(['_token']);
         //商品图片上传
      if($request->hasFile('packageimage')){
         //获取文件上传的后缀名
         $houzhuim = $request->file('packageimage')->getClientOriginalExtension();
         //文件名
         $fileName = md5(time().rand(1,100)).'.'.$houzhuim;
         //获取上传图片路径
         $request->file('packageimage')->move(config::get('app.upload_dir'),$fileName);

         $date['packageimage'] = trim(config::get('app.upload_dir').$fileName,'.');

      }
         $id =$date['sgsid'];
        $pro = DB::table('goods')->where('id',$id)->first();
        //dd($pro);
        $packagepro = $pro->proname;
        $date['packagepro'] = $packagepro;
        //dd($date);
         //执行添加
      $res = DB::table('package')->update($date);
      //dd($res);

     if($res){
         return redirect('admin/goods/packagelist')->with('success','套餐修改成功');
       }else{
         //失败 闪存数据
         return back()->with('error','套餐修改插入失败');
       }

      }

      //******************************************************************//
   /**
    * 套餐内商品管理
    */
    //套餐商品添加添加
   public function getPackagegoodsadd(Request $request)
   {
    //echo "2222";
    $goods = DB::table('goods')->get();
    $package = DB::table('package')->get();
    ///dd($goods);
    return view('goods.packagegoodsadd',['goods'=>$goods,'package'=>$package]);
   }

   //套餐商品添加插入
    public function postPackagegoodsinsert(Request $request)
    {

       $date = $request->except(['_token']);
        //通过套餐的id和商品的id 分贝查出其对应的套餐名和商品名
        $id =$date['tgsid'];
        $pid =$date['peid'];
        $pro = DB::table('goods')->where('id',$id)->first();
        $pgname = DB::table('package')->where('id',$pid)->first();
        $things = $pro->proname;
        $stname = $pgname->tname;
        $date['things'] = $things;
        $date['stname'] = $stname;

        //dd($date);


        //执行插入
        $res = DB::table('packagegoods')->insert($date);

        if($res){
         return redirect('admin/goods/packagelistgoods')->with('success','套餐商品添加成功');
       }else{
         //失败 闪存数据
         return back()->with('error','套餐内商品插入失败');
       }
    }

    //执行套餐内商品列表页
    public function getPackagelistgoods(Request $request)
    {
      //echo 'sassdsa';
      $packagelistgoods = DB::table('packagegoods')
    ->where(function($query)use($request){$query->where('things','like','%'.$request->input('keywords').'%');})

      ->paginate($request->input('num',10));

  //dd($package);

    return view('goods.packagelistgoods',['packagelistgoods'=>$packagelistgoods,'request'=>$request->all()]);
    }

    //套餐内商品的删除

    public function getPackagedelgoods($id)
    {
      //echo 'asdasd';
      //执行删除
      $res = DB::table('packagegoods')->where('id',$id)->delete();
      if($res){
         return redirect('/admin/goods/packagelistgoods')->with('success','套餐内商品删除成功');
       }else{
         //失败 闪存数据
         return back()->with('error','套餐内商品删除失败');
       }
    }

    //套餐中商品的修改

    public function getPackageeditgoods($id)
    {
      //echo "string";
      //修改
      $res = DB::table('packagegoods')->where('id',$id)->first();
      $goods = DB::table('goods')->get();
      $package = DB::table('package')->get();
      

      //显示模板 分配变量
       //dd($res);
      return view('goods.packageeditgoods',['packagegoodsinfo'=>$res,'goods'=>$goods,'packageinfo'=>$package]);

//,'goods'=>$goods,'packageinfo'=>$package

    }

    //执行更改
    public function postPackagegoodsupdate(Request $request)
    {
      //echo "string";
      //dd($request->all());
      $date = $request->except(['_token']);
        //通过套餐的id和商品的id 分贝查出其对应的套餐名和商品名
        $idg =$date['tgsid'];
        $pid =$date['peid'];
        $pro = DB::table('goods')->where('id',$idg)->first();
        $pgname = DB::table('package')->where('id',$pid)->first();
        $things = $pro->proname;
        $stname = $pgname->tname;
        $date['things'] = $things;
        $date['stname'] = $stname;

        //dd($date);

        $id = $date['id'];
        //执行插入
        
        $res = DB::table('packagegoods')->where('id',$id)->update($date);

        if($res){
         return redirect('admin/goods/packagelistgoods')->with('success','套餐商品添加成功');
       }else{
         //失败 闪存数据
         return back()->with('error','套餐内商品插入失败');
       }


    }
/***************************************************************************************************/
    //商品属性添加
    
   public function getKucunadd(Request $request)
   {
    //echo "2222";
    $goods = DB::table('goods')->get();
   
    ///dd($goods);
    return view('goods.kucunadd',['goods'=>$goods]);
   }

   //商品属性数据存入数据库

    public function postKucuninsert(Request $request)
    {

      // dd($request->all());
       $date = $request->except(['_token']);
        $gid = $date['gid'];
        $pro = DB::table('goods')->where('id',$gid)->first();
       
        $proname = $pro->proname;
    
        $date['proname'] = $proname;
     


               
       //dd($date);

        //执行插入
        $res = DB::table('kucun')->insert($date);
       //dd($res);
        if($res){
         return redirect('admin/goods/kucunlist')->with('success','套餐商品添加成功');
       }else{
         //失败 闪存数据
         return back()->with('error','套餐内商品插入失败');
       }
    }


//商品属性列表
     public function getKucunlist(Request $request)
    {
      
      $kucunlist = DB::table('kucun')
    ->where(function($query)use($request){$query->where('proname','like','%'.$request->input('keywords').'%');})

      ->paginate($request->input('num',10));

  //dd($package);

    return view('goods.kucunlist',['kucunlist'=>$kucunlist,'request'=>$request->all()]);
    }

    //商品属性删除
     public function getKucundel($id)
    {
      //echo 'asdasd';
      //执行删除
      $res = DB::table('kucun')->where('id',$id)->delete();
      if($res){
         return redirect('/admin/goods/kucunlist')->with('success','套餐内商品删除成功');
       }else{
         //失败 闪存数据
         return back()->with('error','套餐内商品删除失败');
       }
    }

    //商品属性修改
     public function getKucunedit($id)
        {
          //echo "string";
          //修改
          $res = DB::table('kucun')->where('id',$id)->first();
         
        
          //显示模板 分配变量
         
          return view('goods.kucunedit',['kucunedit'=>$res]);



        }

 //执行更改
    public function postKucunupdate(Request $request)
    {
      //echo "string";
      //dd($request->all());
        $date = $request->except(['_token']);
        //通过套餐的id和商品的id 分贝查出其对应的套餐名和商品名
       

        $id = $date['id'];
        //执行插入
        
        $res = DB::table('kucun')->where('id',$id)->update($date);
        //dd($res);
        if($res){
         return redirect('admin/goods/kucunlist')->with('success','套餐商品修改成功');
       }else{
         //失败 闪存数据
         return back()->with('error','套餐内商品修改失败');
       }


    }

/********************************************************************************************/
//商品规格管理
//商品属性添加
    
   public function getGuigeadd(Request $request)
   {
    //echo "2222";
    $goods = DB::table('goods')->get();
   
    ///dd($goods);
    return view('goods.guigeadd',['goods'=>$goods]);
   }

   //商品属性数据存入数据库

    public function postGuigeinsert(Request $request)
    {

      // dd($request->all());
       $date = $request->except(['_token']);
       //商品图片上传
      if($request->hasFile('image')){
         //获取文件上传的后缀名
         $houzhuim = $request->file('image')->getClientOriginalExtension();
         //文件名
         $fileName = md5(time().rand(1,100)).'.'.$houzhuim;
         //获取上传图片路径
         $request->file('image')->move(config::get('app.upload_dir'),$fileName);

         $date['image'] = trim(config::get('app.upload_dir').$fileName,'.');

      }
        $gid = $date['gid'];
        $pro = DB::table('goods')->where('id',$gid)->first();
       
        $proname = $pro->proname;
    
        $date['proname'] = $proname;
     


               
       //dd($date);

        //执行插入
        $res = DB::table('guige')->insert($date);
       //dd($res);
        if($res){
         return redirect('admin/goods/guigelist')->with('success','套餐商品添加成功');
       }else{
         //失败 闪存数据
         return back()->with('error','套餐内商品插入失败');
       }
    }


//商品属性列表
     public function getGuigelist(Request $request)
    {
      
      $guigelist = DB::table('guige')
    ->where(function($query)use($request){$query->where('proname','like','%'.$request->input('keywords').'%');})

      ->paginate($request->input('num',10));

  //dd($package);

    return view('goods.guigelist',['guigelist'=>$guigelist,'request'=>$request->all()]);
    }



    //商品属性删除
     public function getGuigedel($id)
    {
      //echo 'asdasd';
      //执行删除
      $res = DB::table('guige')->where('id',$id)->delete();
      if($res){
         return redirect('/admin/goods/guigelist')->with('success','删除成功');
       }else{
         //失败 闪存数据
         return back()->with('error','删除失败');
       }
    }

    //商品属性修改
     public function getGuigeedit($id)
        {
          //echo "string";
          //修改
          $res = DB::table('guige')->where('id',$id)->first();
         
        
          //显示模板 分配变量
         
          return view('goods.guigeedit',['guigeedit'=>$res]);



        }

 //执行更改
    public function postGuigeupdate(Request $request)
    {
      //echo "string";
      //dd($request->all());
        $date = $request->except(['_token']);
        //通过套餐的id和商品的id 分贝查出其对应的套餐名和商品名\
         //商品图片上传
      if($request->hasFile('image')){
         //获取文件上传的后缀名
         $houzhuim = $request->file('image')->getClientOriginalExtension();
         //文件名
         $fileName = md5(time().rand(1,100)).'.'.$houzhuim;
         //获取上传图片路径
         $request->file('image')->move(config::get('app.upload_dir'),$fileName);

         $date['image'] = trim(config::get('app.upload_dir').$fileName,'.');

      }
       

        $id = $date['id'];
        //执行插入
        
        $res = DB::table('guige')->where('id',$id)->update($date);
        //dd($res);
        if($res){
         return redirect('admin/goods/guigelist')->with('success','修改成功');
       }else{
         //失败 闪存数据
         return back()->with('error','修改失败');
       }


    }











}
